package cn.edu.lsnu.mbti.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {
    //7天过期
    private static long expire = 604800;
    //32位密钥
    private static String secret = "leshanshifanxueyuanshizhendeniu!";

    //生成token
    public static String generateToken(String username){
        Date now = new Date(); //现在时间
        Date expiration = new Date(now.getTime() + 1000 * expire); //到期时间
        return Jwts.builder()
                .setHeaderParam("type","JWT")
                .setSubject(username)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS512,secret)
                .compact();
    }

    //解析token
    public static Claims getClaimsByToken(String token){
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }
}
